﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Access.PowerTools.AbstractionLayer;
using SMSConsulting.SMSLIB.Utilities;
using Access.PowerTools.Utilities;

namespace Access.PowerTools.Database.BackUp.Plug_In
{
    public class BackUpAgent
    {
        private AccessApplication _application;
        public BackUpAgent(AccessApplication application)
        {
            _application = application; 
        }

        public void SaveAsText(
             AccessDataBaseObjectTypeEnumeration objectType,
             string objectName,
             string backUpFileFullPath)
        {
            AccessDatabaseObjectInfo objectInfo =
                _application.CurrentDatabase.GetObjectInfo(objectType, objectName);
            RunTimeAssert.NotNull(objectInfo,
                string.Format("Can't find object to backup {0} => {1}",objectType, objectName));
            
            _application.SaveAsText(objectType, objectName, backUpFileFullPath);     
        }

        public EventHandler<RunProgressEventArgs> ReportProgressStatus;

        public void BackUpDatabase(
             string backUpFolderPath)
        {
            BackUpDatabase(backUpFolderPath, null);  
        }

        public bool Cancelled { get; private set; }
        public void Cancel()
        {
            Cancelled = true;
        }
        public bool Failed { get; private set; }
        public string ErrorMessage { get; private set; }


        public void BackUpDatabase(
             string backUpFolderPath,
             List<AccessDatabaseObjectInfo> objectsToBackUp)
        {
            Cancelled = false;
            Failed = false;
            ErrorMessage = "";

            try
            {
                if (objectsToBackUp == null)
                    objectsToBackUp = _application.CurrentDatabase.AllObjects;

                int count = 0;
                int maxCount = _application.CurrentDatabase.AllObjects.Count;

                if (!backUpFolderPath.EndsWith(@"\"))
                    backUpFolderPath += System.IO.Path.DirectorySeparatorChar;

                foreach (AccessDatabaseObjectInfo objectInfo in objectsToBackUp)
                {
                    System.Threading.Thread.Sleep(10);
                    if (Cancelled) return;

                    count++;
                    string sourceFileFullPath =
                        objectInfo.SimulationSeedObjectInfoFileFullPath;

                    string backUpFileFullPath =
                        System.IO.Path.Combine(
                        backUpFolderPath,
                        AbstractionLayer.AccessDatabaseObjectInfo.GetBackUpFileName(
                           objectInfo.Type, objectInfo.Name
                           ));

                    if (ReportProgressStatus != null)
                    {
                        string message = string.Format(
                            "Exporting {0} '{1}' - {2} of {3}...",
                            objectInfo.Type,
                            objectInfo.Name,
                            count, maxCount);
                        RunProgressEventArgs e = new RunProgressEventArgs();
                        e.CurrentCountValue = count;
                        e.MaxCountValue = maxCount;
                        e.Message = message;
                        ReportProgressStatus(this, e);
                    }

                    this.SaveAsText(objectInfo.Type, objectInfo.Name, backUpFileFullPath);
                }
            }
            catch (Exception ex)
            {
                Failed = true;
                ErrorMessage = ex.Message; 
            }
        }

    }
}
